<?php

namespace Admin\Controller;

class AdminController extends CommonController
{
    /**
     * 管理员列表
     */
    public function lst()
    {
        $admin = D('admin');
        $count = $admin->where('state != 2')->count();       //查询满足要求的总记录数
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $list = $admin->where('state != 2')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }


    /**
     * 管理员添加
     */
    public function add()
    {
        $admin = D('admin');
        if (IS_POST) {
            $data['username'] = I('username');
            $data['password'] = I('password');
            $data['nickname'] = I('nickname');
            if ($admin->create($data)) {
                $data['password'] = md5($data['password']);
                if ($admin->add($data)) {
                    $this->success('管理员添加成功！', U('lst'));
                } else {
                    $this->error('管理员添加失败！');
                }
            } else {
                $this->error($admin->getError());
            }
            return;
        }
        $this->display();
    }

    /**
     * 管理员修改
     */
    public function edit()
    {
        $admin = D('admin');
        $id = I('id');
        if (IS_POST) {
            if ($id == 1) {
                $this->error('初始化管理员不能修改！');
            } else {
                $data['id'] = I('id');
                $data['username'] = I('username');
                $data['nickname'] = I('nickname');
                $admins = $admin->find($data['id']);
                $password = $admins['password'];
                if (I('password')) {
                    $data['password'] = md5(I('password'));
                } else {
                    $data['password'] = $password;
                }
                if ($admin->create($data)) {
                    if ($admin->save()) {
                        $this->success('修改管理员成功！', U('lst'));
                    } else {
                        $this->error('修改管理员失败！');
                    }
                } else {
                    $this->error($admin->getError());
                }
            }
            return;
        }
        $admins = $admin->find(I('id'));
        $this->assign('admins', $admins);
        $this->display();
    }

    /**
     * 管理员删除
     */
    public function del()
    {
        $admin = D('admin');
        $id = I('id');
        if ($id == 1) {
            $this->error('初始化管理员不能删除！');
        } else {
            $data = $admin->find($id);
            $data['state'] = 2;
            if ($admin->save($data)) {
                $this->success('删除管理员成功！', U('lst'));
            } else {
                $this->error('删除管理员失败！');
            }
        }

    }

    /**
     * 管理员禁用
     */
    public function disable()
    {
        $admin = D('admin');
        $id = I('id');
        if ($id == 1) {
            $this->error('初始化管理员不能禁用！');
        } else {
            $date = $admin->find($id);
            $date['state'] = 3;
            if ($admin->save($date)) {
                $this->success('禁用管理员成功！', U('lst'));
            } else {
                $this->error('禁用管理员失败！');
            }
        }
    }

    /**
     * 管理员启用
     */
    public function enable()
    {
        $admin = D('admin');
        $data = $admin->find(I('id'));
        $data['state'] = 1;
        if ($admin->save($data)) {
            $this->success('启用管理员成功！', U('lst'));
        } else {
            $this->error('启用管理员失败！');
        }
    }

    /**
     * 注销
     */
    public function logout()
    {
        session(null);
        $this->success('退出成功，跳转中...', U('Login/index'));
    }
}